﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text.RegularExpressions;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.LIB;

namespace YArchitech.Public.ExcelToView
{
	internal class SheetViewMakeManger
	{
		internal SheetViewMakeManger(Autodesk.Revit.DB.Document m_doc)
		{
			this.m_doc = m_doc;
			this.LoadFamily();
			this.thisViewMaker = new ViewMaker(m_doc);
		}

		internal List<bool> MakeView(string inputPath, string sheetName, ExternalCommandData commandData)
		{
			return new PreviewData(null, commandData).Lstblresult;
		}

		public void LoadFamily()
		{
			bool flag = false;
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(this.m_doc);
			filteredElementCollector.OfClass(typeof(Family)).ToElements();
			IEnumerator<Element> enumerator = filteredElementCollector.GetEnumerator();
			{
				while (enumerator.MoveNext())
				{
					if (((Family)enumerator.Current).Name.CompareTo("HyFram") == 0)
					{
						flag = true;
						break;
					}
				}
			}
			if (!flag)
			{
				string versionNumber = this.m_doc.Application.VersionNumber;
				string text = Path.Combine(Product.FamilyLocation, versionNumber, "PictureFrame", "HyFram.rft");
				Transaction transaction = new Transaction(this.m_doc, "Load Family");
				try
				{
					transaction.Start();
					this.m_doc.LoadFamily(text);
					transaction.Commit();
				}
				catch (Exception)
				{
					if (transaction.HasStarted())
					{
						transaction.RollBack();
					}
				}
			}
		}

		private void familyLoad()
		{
			bool flag = false;
			List<FamilySymbol> list = new List<FamilySymbol>();
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(this.m_doc);
			filteredElementCollector.OfClass(typeof(FamilySymbol));
			filteredElementCollector.OfCategory(BuiltInCategory.OST_TitleBlocks);
			foreach (Element element in filteredElementCollector.ToElements())
			{
				list.Add(element as FamilySymbol);
			}
			foreach (FamilySymbol familySymbol in list)
			{
				string familyName = familySymbol.FamilyName;
				foreach (string pattern in StaticResources.HyTitleBlockNameBase)
				{
					if (Regex.IsMatch(familyName, pattern))
					{
						flag = true;
						break;
					}
				}
			}
			if (!flag)
			{
				Transaction transaction = new Transaction(this.m_doc, "loadFamily");
				transaction.Start();
				try
				{
					this.m_doc.LoadFamily(StaticResources.PathOfTitleBlockFamiyName + StaticResources.TitleBlockFamilyName);
				}
				catch (Exception)
				{
					transaction.Commit();
				}
				transaction.Commit();
			}
		}

		private Document m_doc;

		private ViewMaker thisViewMaker;
	}
}
